<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="Hexo Theme Keep">
    <meta name="description" content="宋标的个人博客">
    <meta name="author" content="宋标">
	<meta name="referrer" content="no-referrer"/>
    
    <title>
        
            非对称加密与数字证书 |
        
        宋标的blog
    </title>
    
<link rel="stylesheet" href="/css/style.css">

    <link rel="shortcut icon" href="/images/logo.svg">
    
<link rel="stylesheet" href="/css/font-awesome.min.css">

    <script id="hexo-configurations">
    let KEEP = window.KEEP || {};
    KEEP.hexo_config = {"hostname":"song_biao.gitee.io","root":"/","language":"zh-CN","path":"search.xml"};
    KEEP.theme_config = {"toc":{"enable":true,"number":true,"expand_all":true,"init_open":true},"style":{"primary_color":"#0066CC","avatar":"/images/avatar.svg","favicon":"/images/logo.svg","article_img_align":"left","left_side_width":"260px","content_max_width":"920px","hover":{"shadow":false,"scale":false},"first_screen":{"enable":true,"background_img":"/images/bg.svg","description":"while(alive()) study();"},"scroll":{"progress_bar":{"enable":false},"percent":{"enable":false}}},"local_search":{"enable":true,"preload":false},"code_copy":{"enable":false,"style":"default"},"pjax":{"enable":false},"lazyload":{"enable":false},"version":"3.4.5"};
    KEEP.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
  </script>
<meta name="generator" content="Hexo 6.1.0"></head>


<body>
<div class="progress-bar-container">
    

    
</div>


<main class="page-container">

    

    <div class="page-main-content">

        <div class="page-main-content-top">
            <header class="header-wrapper">

    <div class="header-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                宋标的blog
            </a>
        </div>

        <div class="right">
            <div class="pc">
                <ul class="menu-list">
                    
                        <li class="menu-item">
                            <a class=""
                               href="/"
                            >
                                首页
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/tags"
                            >
                                标签
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/categories"
                            >
                                分类
                            </a>
                        </li>
                    
                        <li class="menu-item">
                            <a class=""
                               href="/archives"
                            >
                                归档
                            </a>
                        </li>
                    
                    
                        <li class="menu-item search search-popup-trigger">
                            <i class="fas fa-search"></i>
                        </li>
                    
                </ul>
            </div>
            <div class="mobile">
                
                    <div class="icon-item search search-popup-trigger"><i class="fas fa-search"></i></div>
                
                <div class="icon-item menu-bar">
                    <div class="menu-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <div class="header-drawer">
        <ul class="drawer-menu-list">
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/">首页</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/tags">标签</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/categories">分类</a>
                </li>
            
                <li class="drawer-menu-item flex-center">
                    <a class=""
                       href="/archives">归档</a>
                </li>
            
        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="page-main-content-middle">

            <div class="main-content">

                
                    <div class="fade-in-down-animation">
    <div class="article-content-container">

        <div class="article-title">
            <span class="title-hover-animation">非对称加密与数字证书</span>
        </div>

        
            <div class="article-header">
                <div class="avatar">
                    <img src="/images/avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">宋标</span>
                        
                            <span class="author-label">Lv5</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fas fa-edit"></i>&nbsp;
        <span class="pc">2022-04-05 21:26:43</span>
        <span class="mobile">2022-04-05 21:26</span>
    </span>
    
        <span class="article-categories article-meta-item">
            <i class="fas fa-folder"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/categories/%E5%AF%86%E7%A0%81%E5%AD%A6/">密码学</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    
    
        <span class="article-tags article-meta-item">
            <i class="fas fa-tags"></i>&nbsp;
            <ul>
                
                    <li>
                        <a href="/tags/%E5%AF%B9%E7%A7%B0-%E9%9D%9E%E5%8A%A0%E5%AF%86/">对称/非加密</a>&nbsp;
                    </li>
                
            </ul>
        </span>
    

    
    
    
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h2 id="一、信息传递"><a href="#一、信息传递" class="headerlink" title="一、信息传递"></a>一、信息传递</h2><p>事情是这样的,小明在青春懵懂的时候迷恋上了小红。<br><img src="https://img-blog.csdnimg.cn/20210612135732929.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI2MDA3MjI1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>但得知小红和小华走的很近，并且每天都会发送短信聊天，小明很想知道小华和小红到底在聊些什么。</p>
<h3 id="1-信息窃取"><a href="#1-信息窃取" class="headerlink" title="1.信息窃取"></a>1.信息窃取</h3><p>在一次偶然的机会，小明打开了小红的手机，并查看了小红和小华的聊天记录，于是小明窃取到了小红和小华的聊天信息。</p>
<h3 id="2-信息加密"><a href="#2-信息加密" class="headerlink" title="2.信息加密"></a>2.信息加密</h3><p>而同学小赖将看到的这一切告诉了小红，小红很生气，很害怕信息再次泄露，于是，他和小华决定对信息进行加密，就算信息被小明看到也无济于事。</p>
<h2 id="二、对称加密"><a href="#二、对称加密" class="headerlink" title="二、对称加密"></a>二、对称加密</h2><p>聪明的小华想到，只要和小红事先约定好使用一个<strong>密钥</strong>。这样，小红把要发送的信息加密后发送，小华对接收的密文再用密钥进行解密。</p>
<blockquote>
<p><strong>对称加密</strong>，常用于密码加解密，应用场景相对简单，仅仅一个密钥。</p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/20210612141757109.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI2MDA3MjI1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h3 id="1-密钥泄露"><a href="#1-密钥泄露" class="headerlink" title="1.密钥泄露"></a>1.密钥泄露</h3><p>很不凑巧的是，就在小华将密钥告诉小红的时候，被同学小赖给听到了。放学后，小赖以5根冰淇淋的代价告诉了小明。</p>
<blockquote>
<p>密钥的传递过程避免不了密钥的泄露，除非该密钥不传递</p>
</blockquote>
<p>小华知道后，很是懊恼，决定换一个密钥，但是聪明的小华想了想，有没有办法即使小明得到了密钥，小明也不能破译其中的信息呢?</p>
<h2 id="三、非对称加密"><a href="#三、非对称加密" class="headerlink" title="三、非对称加密"></a>三、非对称加密</h2><p>小华一夜未眠，在天亮之时，灵机一闪。<br>他发明了两个密钥，称为<strong>私钥</strong>和<strong>公钥</strong>。</p>
<blockquote>
<p>私钥可以解密公钥加密的信息，公钥也可以解密私钥加密的信息</p>
</blockquote>
<p>他首先将公钥告诉了小红，自己保留着除了上帝以外没有人知道的私钥，小红得到公钥后，将信息加密后发送给小华，小华再用私钥进行解密。<br><img src="https://img-blog.csdnimg.cn/20210612144538728.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI2MDA3MjI1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"><br>即使小明能得到公钥，也不能对小红加密的信息进行破译。但是小华这时私钥加密的信息，就可能被拿到公钥的小明破解，于是，就需要小红再创建一对密钥，小红自己保留着私钥，将公钥告诉小华，小华再用小红的公钥加密信息后发送给小红，小红接受到后再用自己的私钥进行解密。</p>
<h2 id="四、进阶选读"><a href="#四、进阶选读" class="headerlink" title="四、进阶选读"></a>四、进阶选读</h2><h3 id="1-数字签名"><a href="#1-数字签名" class="headerlink" title="1.数字签名"></a>1.数字签名</h3><p>小华为了确保原信息没有被更改过，而给小红造成误解，于是小华将原信息进行hash，得到唯一的hash值，称为<strong>信息摘要</strong>，再用自己的私钥对hash值进行加密。<br>小华将原信息用小红的公钥进行加密后，连同信息摘要一同发送给小红（<strong>密文+信息摘要</strong>）。<br><img src="https://img-blog.csdnimg.cn/20210612151409837.png" alt="在这里插入图片描述"><br><strong>（上图中仅进行数字签名！）</strong></p>
<p>小红收到小华发送的信息后，首先用私钥将密文解密，将解密后的信息进行hash得到hash值，再通过小华的公钥对加密的信息摘要解密，得到小华书写的原信息的hash值，将两个hash值进行对比。</p>
<blockquote>
<p>如果hash值一致，表示原信息没有被修改，否则信息是被修改过的</p>
</blockquote>
<p>小红便可舍弃这个修改过的信息。</p>
<h3 id="2-数字证书"><a href="#2-数字证书" class="headerlink" title="2.数字证书"></a>2.数字证书</h3><p>可是让小华没想到的是，狡猾的小明也自己创建了一对密钥对，<strong>在小华给小红传递公钥的过程中，公钥被换成了小明自己的公钥</strong>，小红实际收到的是小明的公钥。这样小明便可以偷天换日，顶替了小华，这一举动称为<strong>中间人攻击</strong>。</p>
<p>让接收方知情发送者，解决的办法有两个。</p>
<h4 id="2-1公开公钥"><a href="#2-1公开公钥" class="headerlink" title="2.1公开公钥"></a>2.1公开公钥</h4><p>小华公开自己的公钥，小红在验证数字签名前，就已经知道小华的公钥是什么。</p>
<h4 id="2-2数字证书"><a href="#2-2数字证书" class="headerlink" title="2.2数字证书"></a>2.2数字证书</h4><p>公正的班长(证书中心)也有一对密钥对，并且在班上<strong>公开了自己的公钥</strong>。<br>小华再给小红发送信息时，只需要将小华自己的<strong>公钥、信息和数字签名</strong>一同交给班长。<br>班长用自己的私钥加密后，便得到真正可信的<strong>数字证书</strong>。<br>小华将其发送给小红，小红便用班长公开的公钥进行解密，真正得到了小华的公钥和相关信息。<br>小明知道班长公正无私，已经哭晕在了厕所。</p>
<h2 id="五、总结"><a href="#五、总结" class="headerlink" title="五、总结"></a>五、总结</h2><p>笔者的理解，只要密钥涉及传递，一定避免不了密钥泄露。最常用的方式便是非对称加密，如目前的RSA加密算法。<br>https也是采用数字证书的方式，笔者没有深入去了解，就不展开了。</p>
<p>文章写到最后，笔者能力有限，文章存在知识误区，还请不吝指出，若文章对你有帮助，不妨点个赞对笔者支持一下把(●’◡’●)</p>
<p><em>文章图片素材全部来源于网络，侵权请联系我删除。</em> </p>

        </div>

        

        
            <ul class="post-tags-box">
                
                    <li class="tag-item">
                        <a href="/tags/%E5%AF%B9%E7%A7%B0-%E9%9D%9E%E5%8A%A0%E5%AF%86/">#对称/非加密</a>&nbsp;
                    </li>
                
            </ul>
        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                           rel="prev"
                           href="/2022/04/06/leetcode206-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8/"
                        >
                            <span class="left arrow-icon flex-center">
                              <i class="fas fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">leetcode206. 反转链表</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                           rel="next"
                           href="/2022/04/05/redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E5%AE%8C%E6%95%B4%E6%94%BB%E7%95%A5/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">redis主从复制完整攻略</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                              <i class="fas fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        

        
            <div class="comment-container">
                <div class="comments-container">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fas fa-comments">&nbsp;评论</i>
    </div>
    

        
            
    <div class="valine-container">
        <script 
                src="//cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script>
        <div id="vcomments"></div>
        <script >
            function loadValine() {
                new Valine({
                    el: '#vcomments',
                    appId: 'GaPFQduCGtJWQ2Cmma9jEfx1-gzGzoHsz',
                    appKey: 'h9IWwxU3RWqBFCD4CqERkpuv',
                    meta: ['nick', 'mail', 'link'],
                    avatar: 'wavatar',
                    enableQQ: true,
                    placeholder: '',
                    lang: 'zh-CN'.toLowerCase()
                });

                function getAuthor(language) {
                    switch (language) {
                        case 'en':
                            return 'Author';
                        case 'zh-CN':
                            return '博主';
                        default:
                            return 'Master';
                    }
                }

                // Add "Author" identify
                const getValineDomTimer = setInterval(() => {
                    const vcards = document.querySelectorAll('#vcomments .vcards .vcard');
                    if (vcards.length > 0) {
                        let author = '宋标';

                        if (author) {
                            for (let vcard of vcards) {
                                const vnick_dom = vcard.querySelector('.vhead .vnick');
                                const vnick = vnick_dom.innerHTML;
                                if (vnick === author) {
                                    vnick_dom.innerHTML = `${vnick} <span class="author">${getAuthor(KEEP.hexo_config.language)}</span>`
                                }
                            }
                        }
                        clearInterval(getValineDomTimer);
                    } else {
                        clearInterval(getValineDomTimer);
                    }
                }, 2000);
            }

            if ('false') {
                const loadValineTimeout = setTimeout(() => {
                    loadValine();
                    clearTimeout(loadValineTimeout);
                }, 1000);
            } else {
                window.addEventListener('DOMContentLoaded', loadValine);
            }
        </script>
    </div>



        
    
</div>

            </div>
        
    </div>
</div>


                
            </div>

        </div>

        <div class="page-main-content-bottom">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info info-item">
            &copy;
            
              <span>2020</span>
              -
            
            2023&nbsp;<i class="fas fa-heart icon-animate"></i>&nbsp;<a href="/">宋标</a>
        </div>
        
        <div class="theme-info info-item">
            由 <a target="_blank" href="https://hexo.io">Hexo</a> 驱动&nbsp;|&nbsp;主题&nbsp;<a class="theme-version" target="_blank" href="https://github.com/XPoet/hexo-theme-keep">Keep v3.4.5</a>
        </div>
        
        
    </div>
</footer>

        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="tools-list">
        <!-- TOC aside toggle -->
        
            <li class="tools-item page-aside-toggle">
                <i class="fas fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fas fa-comment"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-bottom-side-tools">
        <div class="side-tools-container">
    <ul class="side-tools-list">
        <li class="tools-item tool-font-adjust-plus flex-center">
            <i class="fas fa-search-plus"></i>
        </li>

        <li class="tools-item tool-font-adjust-minus flex-center">
            <i class="fas fa-search-minus"></i>
        </li>

        <li class="tools-item tool-expand-width flex-center">
            <i class="fas fa-arrows-alt-h"></i>
        </li>

        <li class="tools-item tool-dark-light-toggle flex-center">
            <i class="fas fa-moon"></i>
        </li>

        <!-- rss -->
        

        
            <li class="tools-item tool-scroll-to-top flex-center">
                <i class="fas fa-arrow-up"></i>
            </li>
        

        <li class="tools-item tool-scroll-to-bottom flex-center">
            <i class="fas fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="exposed-tools-list">
        <li class="tools-item tool-toggle-show flex-center">
            <i class="fas fa-cog fa-spin"></i>
        </li>
        
    </ul>
</div>

    </div>

    
        <aside class="page-aside">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E4%BF%A1%E6%81%AF%E4%BC%A0%E9%80%92"><span class="nav-number">1.</span> <span class="nav-text">一、信息传递</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E4%BF%A1%E6%81%AF%E7%AA%83%E5%8F%96"><span class="nav-number">1.1.</span> <span class="nav-text">1.信息窃取</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E4%BF%A1%E6%81%AF%E5%8A%A0%E5%AF%86"><span class="nav-number">1.2.</span> <span class="nav-text">2.信息加密</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86"><span class="nav-number">2.</span> <span class="nav-text">二、对称加密</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%AF%86%E9%92%A5%E6%B3%84%E9%9C%B2"><span class="nav-number">2.1.</span> <span class="nav-text">1.密钥泄露</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E9%9D%9E%E5%AF%B9%E7%A7%B0%E5%8A%A0%E5%AF%86"><span class="nav-number">3.</span> <span class="nav-text">三、非对称加密</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E8%BF%9B%E9%98%B6%E9%80%89%E8%AF%BB"><span class="nav-number">4.</span> <span class="nav-text">四、进阶选读</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E6%95%B0%E5%AD%97%E7%AD%BE%E5%90%8D"><span class="nav-number">4.1.</span> <span class="nav-text">1.数字签名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E6%95%B0%E5%AD%97%E8%AF%81%E4%B9%A6"><span class="nav-number">4.2.</span> <span class="nav-text">2.数字证书</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1%E5%85%AC%E5%BC%80%E5%85%AC%E9%92%A5"><span class="nav-number">4.2.1.</span> <span class="nav-text">2.1公开公钥</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2%E6%95%B0%E5%AD%97%E8%AF%81%E4%B9%A6"><span class="nav-number">4.2.2.</span> <span class="nav-text">2.2数字证书</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E6%80%BB%E7%BB%93"><span class="nav-number">5.</span> <span class="nav-text">五、总结</span></a></li></ol>
    </div>
</div>
        </aside>
    

    <div class="image-viewer-container">
    <img src="">
</div>


    
        <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
          <span class="search-input-field-pre">
            <i class="fas fa-keyboard"></i>
          </span>
            <div class="search-input-container">
                <input autocomplete="off"
                       autocorrect="off"
                       autocapitalize="off"
                       placeholder="搜索..."
                       spellcheck="false"
                       type="search"
                       class="search-input"
                >
            </div>
            <span class="popup-btn-close">
                <i class="fas fa-times"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fas fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>

    

</main>




<script src="/js/utils.js"></script>

<script src="/js/main.js"></script>

<script src="/js/header-shrink.js"></script>

<script src="/js/back2top.js"></script>

<script src="/js/dark-light-toggle.js"></script>



    
<script src="/js/local-search.js"></script>







<div class="post-scripts">
    
        
<script src="/js/left-side-toggle.js"></script>

<script src="/js/libs/anime.min.js"></script>

<script src="/js/toc.js"></script>

    
</div>



</body>
</html>
